//
//  CommentsCell.m
//  WorldOfFacts_V2
//
//  Created by Demjanko Denis on 12.12.12.
//  Copyright (c) 2012 it-dimension. All rights reserved.
//

#import "CommentsCell.h"
#import "UIImageView+WebCache.h"
#import "UserManager.h"
#import "UIImageExtras.h"

@implementation CommentsCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        avatar_border = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 55, 55)];
        avatar_border.image = [UIImage imageNamed:@"ava_border.png"];
        [self addSubview:avatar_border];
        
        avatar = [[UIImageView alloc] initWithFrame:CGRectMake(10, 9, 45, 45)];
        [self addSubview:avatar];
            
        separator = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 2)];
        separator.image = [UIImage imageNamed:@"comments_separator.png"];
        [self addSubview:separator];
        
    
        buble = [[UIImageView alloc] initWithFrame:CGRectMake(60, 25, 245, 41)];
        buble.image = [[UIImage imageNamed:@"MessageBubbleGray"] stretchableImageWithLeftCapWidth:23 topCapHeight:15];
        [self addSubview:buble];
        
        name = [[UILabel alloc] initWithFrame:CGRectMake(65, 4, 230, 25)];
        name.shadowColor = [UIColor whiteColor];
        name.shadowOffset = CGSizeMake(0, 1);
        name.backgroundColor = [UIColor clearColor];
        name.font = [UIFont fontWithName:@"MyriadPro-Regular" size:14];
        name.textColor = [UIColor colorWithRed:76.0f/255.0f green:76.0f/255.0f blue:76.0f/255.0f alpha:1];
        [self addSubview:name];
        
        date = [[UILabel alloc] initWithFrame:CGRectMake(240, 65, 250, 25)];
        date.shadowColor = [UIColor whiteColor];
        date.shadowOffset = CGSizeMake(0, 1);
        date.backgroundColor = [UIColor clearColor];
        date.font = [UIFont fontWithName:@"HelveticaNeue-Light" size:12];
        date.textColor = [UIColor colorWithRed:128.0f/255.0f green:128.0f/255.0f blue:128.0f/255.0f alpha:1];
        [self addSubview:date];
        
        comment = [[UILabel alloc] initWithFrame:CGRectMake(80, 33, 225, 25)];
        comment.shadowColor = [UIColor whiteColor];
        comment.numberOfLines = 0;
        comment.shadowOffset = CGSizeMake(0, 1);
        comment.backgroundColor = [UIColor clearColor];
        comment.font = [UIFont systemFontOfSize:14.0f];
        comment.textColor = [UIColor blackColor];
        comment.lineBreakMode = UILineBreakModeWordWrap;
        [self addSubview:comment];

    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    separator.frame = CGRectMake(0, self.frame.size.height-2, 320, 2);
}

-(void)setComment:(NSMutableDictionary*)_comment{
    CGSize expectedCommentSize = [[_comment valueForKey:@"comment"] sizeWithFont:comment.font  constrainedToSize:CGSizeMake(comment.frame.size.width, 9999) lineBreakMode:comment.lineBreakMode];
    
    comment.frame = CGRectMake(80, 33, 225, expectedCommentSize.height);
    comment.text = [_comment valueForKey:@"comment"];
    
    date.text = [_comment valueForKey:@"date"];
    name.text = [_comment valueForKey:@"user_name"];

    buble.frame = CGRectMake(60, 25, 245, expectedCommentSize.height+15);
    date.frame = CGRectMake(240, buble.frame.size.height+25, 250, 25);
    
    [avatar setImageWithURL:[NSURL URLWithString:[_comment valueForKey:@"user_photo"]] placeholderImage:[UIImage imageNamed:@"empty_avatar"] isScaled:YES size:CGSizeMake(90, 90)];
}

-(void)dealloc{
    avatar = nil;
    avatar_border = nil;
    separator = nil;
    buble = nil;
    name = nil;
    date = nil;
    comment = nil;
}

@end
